package com.irdstudio.efp.nls.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.nls.service.dao.BdLoanpayDao;
import com.irdstudio.efp.nls.service.domain.BdLoanpay;
import com.irdstudio.efp.nls.service.facade.BdLoanpayService;
import com.irdstudio.efp.nls.service.vo.BdLoanpayVO;

/**
 * ServiceImpl: 百度放款申请信息
 * 
 * @author huangwb
 * @since 2018年12月27日 上午8:33:47
 * @version 1.0
 */
@Service("bdLoanpayService")
public class BdLoanpayServiceImpl implements BdLoanpayService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BdLoanpayServiceImpl.class);
	private static final String CHANGE_DATA_ERROR = "数据转换出现异常，异常信息为：";
	@Autowired
	private BdLoanpayDao bdLoanpayDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertBdLoanpay(BdLoanpayVO inBdLoanpayVo) {
		logger.debug("当前新增数据为:" + inBdLoanpayVo.toString());
		int num = 0;
		try {
			BdLoanpay bdLoanpay = new BdLoanpay();
			beanCopy(inBdLoanpayVo, bdLoanpay);
			num = bdLoanpayDao.insertBdLoanpay(bdLoanpay);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(BdLoanpayVO inBdLoanpayVo) {
		logger.debug("当前删除数据条件为:" + inBdLoanpayVo);
		int num = 0;
		try {
			BdLoanpay bdLoanpay = new BdLoanpay();
			beanCopy(inBdLoanpayVo, bdLoanpay);
			num = bdLoanpayDao.deleteByPk(bdLoanpay);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdLoanpayVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(BdLoanpayVO inBdLoanpayVo) {
		logger.debug("当前修改数据为:" + inBdLoanpayVo.toString());
		int num = 0;
		try {
			BdLoanpay bdLoanpay = new BdLoanpay();
			beanCopy(inBdLoanpayVo, bdLoanpay);
			num = bdLoanpayDao.updateByPk(bdLoanpay);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdLoanpayVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public BdLoanpayVO queryByPk(BdLoanpayVO inBdLoanpayVo) {

		logger.debug("当前查询参数信息为:" + inBdLoanpayVo);
		try {
			BdLoanpay querybdLoanpay = new BdLoanpay();
			beanCopy(inBdLoanpayVo, querybdLoanpay);
			BdLoanpay queryRslbdLoanpay = bdLoanpayDao.queryByPk(querybdLoanpay);
			if (Objects.nonNull(queryRslbdLoanpay)) {
				BdLoanpayVO outBdLoanpayVo = beanCopy(queryRslbdLoanpay, new BdLoanpayVO());
				logger.debug("当前查询结果为:" + outBdLoanpayVo.toString());
				return outBdLoanpayVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BdLoanpayVO> queryAllOwner(BdLoanpayVO BdLoanpayVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BdLoanpayVO> list = null;
		try {
			List<BdLoanpay> bdLoanpays = bdLoanpayDao.queryAllOwnerByPage(BdLoanpayVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + bdLoanpays.size());
			pageSet(bdLoanpays, BdLoanpayVo);
			list = (List<BdLoanpayVO>) beansCopy(bdLoanpays, BdLoanpayVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BdLoanpayVO> queryAllCurrOrg(BdLoanpayVO BdLoanpayVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BdLoanpay> bdLoanpays = bdLoanpayDao.queryAllCurrOrgByPage(BdLoanpayVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + bdLoanpays.size());
		List<BdLoanpayVO> list = null;
		try {
			pageSet(bdLoanpays, BdLoanpayVo);
			list = (List<BdLoanpayVO>) beansCopy(bdLoanpays, BdLoanpayVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BdLoanpayVO> queryAllCurrDownOrg(BdLoanpayVO BdLoanpayVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BdLoanpay> bdLoanpays = bdLoanpayDao.queryAllCurrDownOrgByPage(BdLoanpayVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bdLoanpays.size());
		List<BdLoanpayVO> list = null;
		try {
			pageSet(bdLoanpays, BdLoanpayVo);
			list = (List<BdLoanpayVO>) beansCopy(bdLoanpays, BdLoanpayVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByOrderId(BdLoanpayVO inBdLoanpayVo) {
		logger.debug("当前修改数据为:" + inBdLoanpayVo.toString());
		int num = 0;
		try {
			BdLoanpay bdLoanpay = new BdLoanpay();
			beanCopy(inBdLoanpayVo, bdLoanpay);
			num = bdLoanpayDao.updateByOrderId(bdLoanpay);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdLoanpayVo + "修改的数据条数为" + num);
		return num;
	}

	 /**
		 * 
		 * 贷款每日放款明细临时表数据到放款申请信息表有数据则更新，无则新增 <br>
		 * 0.1:weijj:2019年5月7日 下午2:37:18:新增 <br>
		 * 
		 * @author weijj
		 * @since 2019年5月7日 下午2:37:18
		 * @date 2019年5月7日 下午2:37:18
		 * @version 0.1
		 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateBdLoanpay(List<BdLoanpayVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【放款申请信息表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<BdLoanpay> bdLoanpaylList = new ArrayList<BdLoanpay>();
		int num = 0;
		try {
			bdLoanpaylList = (List<BdLoanpay>) beansCopy(list, BdLoanpay.class);
			num = bdLoanpayDao.insertOrUpdateBdLoanpay(bdLoanpaylList);
			String num_msg = "【放款申请信息表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【放款申请信息表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
}
